package com.customization.cronjob;



import com.customization.commons.Console;
import com.customization.webservice.flowtrigger.impl.UpdateCBSPayInfo;
import com.engine.personalIncomeTax.biz.RecordsBiz;
import weaver.conn.RecordSet;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.workflow.webservices.WorkflowRequestInfo;
import weaver.workflow.webservices.WorkflowServiceImpl;

import java.util.UUID;

/**
 * @author liutaihong
 * @version 1.0.0
 * @ClassName SynToDoSomething.java
 * @Description TODO
 * @createTime 2020-06-02 8:45:00
 */
public class SynCbsCron extends BaseCronJob {

    @Override
    public void execute() {
        Console.log("CronJob SynCbsCron execute start");
        this.executeDemo();
        Console.log("CronJob SynCbsCron execute end");

    }

    /**
     * 计划任务实际执行的方法体
     */
    public void runSomeThing(){

        /*
        WorkflowServiceImpl workflowService = new WorkflowServiceImpl();
        String  [] conditions={"t1.workflowid in(105981,99985,102982,100982)"};
        WorkflowRequestInfo[] res= workflowService.getToDoWorkflowRequestList(1,10,10000,13513,conditions);
        Console.log("查询13513的待办数量："+res.length);
        for (WorkflowRequestInfo wr:res ) {
            Console.log("处理："+wr.getRequestId());
            new UpdateCBSPayInfo().submitWorkflow(wr.getRequestId());
        }
        */

        RecordSet  rs=new RecordSet();
        RecordSet recordSet = new RecordSet();

        String sqlwhere=" and (status is null or status not in ('1','-1','2','3')) ";
        //依次处理中间表没有成功的数据;
        // String sql=" select *  from uf_cbs_pay where （status is null or status not in ('1','-1') )   and LINENR='01' "+sqlwhere  +" order by status desc,id desc";
        String sql="select  * from ( select  * from ( select t1.*,row_number() over(partition by wfid order by id desc) rn  from (select *  from uf_cbs_pay where  LINENR='01' "+sqlwhere+" order by   status desc ,id    desc)t1 ) t where rn=1 order by status*1 desc,MODEDATACREATEDATE asc,MODEDATACREATETIME asc  )t2  where  rownum < 50 ";
        Console.log("查询即将处理成功的数据："+sql);
        rs.execute(sql);
        String mainid="0";
        int  count=1;
        while (rs.next()) {
            String  requestid=rs.getString("wfid");
            recordSet.execute("update syncronjob set status='1',datestr='"+System.currentTimeMillis()+"' where syntype='SynCbsCron'");//标记为已经有线程在跑
            Console.log("开始处理:"+requestid);
            new UpdateCBSPayInfo().submitWorkflow(requestid);
        }
        recordSet.execute("update syncronjob set status='0' where syntype='SynCbsCron'");//标记当前同步进程已经结束

        //new UpdateCBSPayInfo().submitWorkflow("");
    }


    /**
     * 通过监控是否有正在执行的计划任务来确保在上一次计划任务未执行完成前，再次触发改计划任务
     */
    private void executeDemo(){

        String uuid= UUID.randomUUID().toString().replace("-","");
        Console.log(" execute BaseCronJob SynCbsCron ================START================= "+uuid);

        long startreadSap = System.currentTimeMillis();

        RecordSet recordSet = new RecordSet();
        recordSet.execute("select * from syncronjob  where syntype='SynCbsCron'");//记录当前是否已经有线程在跑
        boolean syncProcess = false;
        if (recordSet.next()) {
            if (recordSet.getInt("status") == 0) {
                syncProcess = true;
            } else {

                long lastreadSapTime = Long.parseLong(recordSet.getString("datestr"));
                long nowTime = System.currentTimeMillis();
                Console.log("lastreadSapTime：" + lastreadSapTime);
                Console.log("nowTime：" + nowTime);
                Console.log("距离上次同步时间：" + (nowTime - lastreadSapTime) / 1000 / 60);
                if ((nowTime - lastreadSapTime) / 1000 / 60 > 5) {//增加距离最后一次同步时间大于5分的重新可继续执行计划任务
                    syncProcess = true;
                } else {
                    Console.log("当前正在同步中.....  update syncronjob set status='0' ");
                    Console.log("距离上次开始任务没有超过5分钟，请等待上次任务执行完成");
                    Console.log("================END=================");
                }

            }
        } else {
            recordSet.execute("insert  into  syncronjob (status,datestr,syntype) values ('0','" + System.currentTimeMillis() + "','SynCbsCron')");
            syncProcess = true;
        }
        if (syncProcess) {
            recordSet.execute("update syncronjob set status='1',datestr='"+System.currentTimeMillis()+"' where syntype='SynCbsCron'");//标记为已经有线程在跑
            this.runSomeThing();
            recordSet.execute("update syncronjob set status='0' where syntype='SynCbsCron'");//标记当前同步进程已经结束
            Console.log("整体耗时：" + (System.currentTimeMillis() - startreadSap) / 1000 + "s");
            Console.log("================END=================");
        }
        Console.log(" execute BaseCronJob SynCbsCron ================END================= "+uuid);

    }

}
